//CSC330_Alex_Stoyko_Lab4
#include <iostream>
#include <string>
#include "Card.h"
#include "DeckofCards.h"
#include "Hand.h"
#include "Dealer.h"
#include "Player.h"
using namespace std;

int main()
{
	DeckofCards cardDeck;  //Declare card deck
	cardDeck.shuffle();  //shuffle the cards

	// PART 2 -------------------------------------------------------------------------------------
	/*
	vector<Hand> listHand(2);
	for(int h = 0; h < 2; h++)
	  {
		  for(int i = 0; i < 5; i++)
		  {
			Card card = cardDeck.dealCard();  //get card
			listHand[h].AddCard(card);
		  }
	  cout << endl;
	  }
	cout << "Part 2:\n";
	cout << "Hand 1: " << endl;
	listHand[0].Sort();
	listHand[0].Print();
	cout << "\nHand 2: " << endl;
	listHand[1].Sort();
	listHand[1].Print();
	cout << listHand[0].CompareTo(listHand[1]) << endl;
	*/
	// ---------------------------------------------------------------------------------------------

	// PART 3 -------------------------------------------------------------------------------------
	/*
	cout << "\nPart 3:\n";
	Dealer dealer = Dealer();
	for (int i = 0; i < 5; i++)
	{
		Card card = cardDeck.dealCard();
		dealer.addCard(card);
	}
	dealer.Sort();
	cout << dealer.toString() << endl;
	int numChange = dealer.Evaluate();
	if (numChange > 0)
	{
		cout << "Change " << numChange << " cards\n";
		for (int i = 0; i < numChange; i++)
		{
			Card card = cardDeck.dealCard();
			dealer.addCard(card);
		}
		dealer.Sort();
	}
	else
	{
		cout << "Unchanged\n";
	}
	cout << dealer.toString() << endl;
	*/
	// ---------------------------------------------------------------------------------------------

	// PART 4 -------------------------------------------------------------------------------------
	cout << "\nPart 4:\n";
	int numPlayer;
	bool isContinue = false;
	vector<Player> playerList;
	Dealer dealer = Dealer();
	Card card;
	int i, j;
	do
	{
		cout << "Please, enter number of player: ";
		cin >> numPlayer;
		if (numPlayer > 0 && numPlayer < 4)
		{
			// Create player list
			for (i = 0; i < numPlayer; i++)
			{
				cout << "Enter player's name: [" << i + 1 << "] ";
				string name;
				cin >> name;
				Player player = Player(name);
				playerList.push_back(player);
			}

			// Deal card for Dealer and Players
			for (i = 0; i < 5; i++)
			{
				card = cardDeck.dealCard();
				dealer.addCard(card);
				for (j = 0; j < numPlayer; j++)
				{
					card = cardDeck.dealCard();
					playerList[j].addCard(card);
				}
			}

			// Sort dealer's hand
			dealer.Sort();

			// Print player's cards
			cout << endl;
			for (i = 0; i < numPlayer; i++)
			{
				cout << playerList[i].toString();
			}

			// Player change cards
			int numChange = 0;
			int indexChange = 0;
			vector<int> indexList = vector<int>();
			for (i = 0; i < numPlayer; i++)
			{
				indexList.clear();
				do
				{
					cout << "Hi " << playerList[i].getName() << "! Please enter number of cards which are changed [0 - 3 cards]: ";
					cin >> numChange;
					if (numChange > 0 && numChange < 4)
					{
						for (j = 0; j < numChange; j++)
						{
							do
							{
								cout << "Enter index of card which is changed: [0 - 4] ";
								cin >> indexChange;
								if (indexChange >= 0 && indexChange <= 4)
								{
									indexList.push_back(indexChange);
								}
								else
								{
									cout << "\nPlease try again!\n";
								}
							}
							while (indexChange < 0 || indexChange > 4);
						}
						playerList[i].changeCards(indexList);
						for (j = 0; j < numChange; j++)
						{
							card = cardDeck.dealCard();
							playerList[i].addCard(card);
						}
						cout << "\nNew hand:\n" << playerList[i].toString();
					}
				}
				while (numChange < 0 || numChange > 3);
			}

			// Dealer change cards
			numChange = dealer.Evaluate();
			if (numChange > 0)
			{
				for (i = 0; i < numChange; i++)
				{
					card = cardDeck.dealCard();
					dealer.addCard(card);
				}
				cout << "\nDealer change " << numChange << " cards\n";
			}
			else
			{
				cout << "\nDealer don't change his hand\n";
			}

			// Print hands of dealer and players
			cout << "\n-------------------------FINAL---------------------------\n";
			cout << dealer.toString();
			for (i = 0; i < numPlayer; i++)
				cout << playerList[i].toString();

			// Evaluate and Print result of game:
			
		}
		else
		{
			cout << "\nTry again\n";
			isContinue = true;
		}
	}
	while (isContinue);
	// ---------------------------------------------------------------------------------------------

	system("pause"); //console window automatically close after the program has finished
	return 0;
}